/*
 * @Author: lee
 * @Date: 2023-10-25 18:34:06
 * @LastEditors: lee
 * @LastEditTime: 2023-10-25 19:17:15
 * @Description: file content
 */
const fs = require('fs')
const path = require('path')
const svgCaptcha = require("svg-captcha");
const config = require('../config/index')

module.exports = class Common {
    static async createCaptcha(ctx) {
        const verification = await svgCaptcha.create({
            size: 4,
            background: "#eee",
            ignoreChars:'0OlI18B',
            width:100,
            height:40
        });
        ctx.session.verifyCode = verification.text.toLocaleLowerCase()
        return ctx.success({ data: { text: verification.text, svg: verification.data } })
    }

     // 上传图片
    static async uploadImg(ctx) {
		
        let file = ctx.request.files.image;

        let next = file.name.split('.')
        let url = `uploads/${Date.now()}.${next[next.length - 1]}`

        let urlto = path.resolve(__dirname, '../public', url)

        // 流式读取并写入
        const readstream = fs.createReadStream(file.path);
        await readstream.pipe(fs.createWriteStream(urlto));
        return ctx.success({msg:'上传成功',data:`${config.SERVER_URL}/${url}`})
  }
}
